package cn.gson.crmdemo.model.service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONArray;

import cn.gson.crmdemo.model.dao.CustomerDao;
import cn.gson.crmdemo.model.mapper.CustomerMapper;
import cn.gson.crmdemo.model.mapper.SalesOpportunityMapper;
import cn.gson.crmdemo.model.pojo.Customer;
import cn.gson.crmdemo.model.pojo.Emp;
import cn.gson.crmdemo.model.pojo.Salesopportunity;
import cn.gson.crmdemo.model.vo.SaleVo;

@Service
@Transactional(rollbackFor=Exception.class)
public class CustomerService {
	@Autowired
	CustomerDao ctd;
	@Autowired
	CustomerMapper ctm;
	/**
	 * 查所有id和名称
	 * @return
	 */
	public JSONArray selectCustomer(){
		return ctm.selectCustomerName();
	}
	
	/**
	 * 查所有
	 * @return
	 */
	public JSONArray selectCustomerAll(){
		return ctm.selectCustomerAll();
	}
	/**
	 * 删除
	 */
	public void deleteCustomer(Integer customerId) {
		ctm.delCustomerId(customerId);
	}
	/**
	 * 新增
	 * @param dept 由用户提交到Controller
	 */
/*	public void addCus(Customer customer) {
		
		ctd.save(customer);
	}*/
	public boolean addCus(Customer customer) {
		List<String> list = ctm.selectCustomerPhone(customer.getCustomerPhone());
		if(list.size()>0){
			return false;
		}
		else {
			ctd.save(customer);
			return true;
		}
	}
	/**
	 * 修改员工信息
	 */
	
	
	public JSONArray selectCusId(Integer cusId) {
		return ctm.selectCusId(cusId);
	}
	
	public void updateCus(Customer cuss){
		Customer cus= ctd.findById(cuss.getCustomerId()).get();
		
	//	cus.setCustomerId(cuss.getCustomerId());
		cus.setCustomerName(cuss.getCustomerName());
		cus.setCustomerPhone(cuss.getCustomerPhone());
		cus.setCustomerSource(cuss.getCustomerSource());
		cus.setCustomerEmpName(cuss.getCustomerEmpName());
		cus.setCustomerDate(cuss.getCustomerDate());
		cus.setContractTime(cuss.getContractTime());
		cus.setCustomerType(cuss.getCustomerType());
		cus.setPayMoney(cuss.getPayMoney());
		ctd.save(cus);
		
		
	}
	
	public void updateCus1(Integer a){
		ctm.updateSaleState(a);
	}
	public void updateCus2(Integer a){
		ctm.updateQuotation(a);
	}
	public JSONArray selectMoHuCustomerName(String customerName){
		return ctm.selectMoHuCustomerName(customerName);
	}
	public JSONArray selectType(Integer customerType){
		return ctm.selectType(customerType);
	}
	/**
	 * 详情
	 */
	public JSONArray selectXq(Integer saleId) {
		return ctm.selectXq(saleId);
	}
}
